/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.service.merchant;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.service.BaseService;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.modules.ivm.dao.merchant.DeliveryMemberDao;
import com.neusoft.lohas.modules.ivm.entity.merchant.DeliveryMember;
import com.neusoft.lohas.modules.ivm.utils.ProductUtils;

/**
 * 送货员Service
 * @author Jackie.Liu
 * @version 2014-11-03
 */
@Component
@Transactional(readOnly = true)
public class DeliveryMemberService extends BaseService {

	@Autowired
	private DeliveryMemberDao deliveryMemberDao;
	
	public DeliveryMember get(String id) {
		return deliveryMemberDao.get(id);
	}
	
	public Page<DeliveryMember> find(Page<DeliveryMember> page, DeliveryMember deliveryman) {
		DetachedCriteria dc = deliveryMemberDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(deliveryman.getName())){
			dc.add(Restrictions.like("name", "%"+deliveryman.getName()+"%"));
		}
		dc.add(Restrictions.eq(DeliveryMember.FIELD_DEL_FLAG, DeliveryMember.DEL_FLAG_NORMAL));
		dc.add(Restrictions.eq("createBy", deliveryman.getCreateBy()));
		dc.add(Restrictions.eq("type", deliveryman.getType()));
		dc.addOrder(Order.asc("createDate"));
		return deliveryMemberDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(DeliveryMember deliveryman) {
		deliveryMemberDao.save(deliveryman);
		ProductUtils.removeCache(ProductUtils.CACHE_DELIVERYMEMBER_LIST);
		ProductUtils.removeCache(ProductUtils.CACHE_PURCHASEMEMBER_LIST);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		deliveryMemberDao.deleteById(id);
		ProductUtils.removeCache(ProductUtils.CACHE_DELIVERYMEMBER_LIST);
		ProductUtils.removeCache(ProductUtils.CACHE_PURCHASEMEMBER_LIST);
	}

	public Object findByName(int type, String name, String mId) {
		return deliveryMemberDao.findByName(type, name, mId);
	}
}
